﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Raise3000.Code.HandHistoryLoader
{
    public class Tokenizer
    {
        string textToParse_ = null;

        int state_ = 1;

        int at_ = 0;

        public Tokenizer()
        {
        }

        public void ReInitTokenizer(string text)
        {
            textToParse_ = text;
            state_ = 1;
            at_ = 0;
        }

        public string GetNextToken()
        {
            string nextToken = "";

            bool getNextToken = false;



            while (!getNextToken && at_ < textToParse_.Length)
            {
                char c = textToParse_[at_];

                switch (state_)
                {
                    case 1:
                        {
                            // if c is char or number, means belongs to current token
                            if (IsChar(c) || IsNumber(c) || (c == '.') || (c == '*') || (c == '_'))
                            {
                                nextToken += c;
                                ++at_;
                            }
                            else if (IsSeparatorCharacter(c))
                            {
                                getNextToken = (nextToken != "");
                                state_ = 2;

                            }
                            else if (c == ' ') // this is also a separator, but does not have to deal with.
                            {
                                ++at_;
                                getNextToken = (nextToken != "");
                            }
                            else
                            {
                                // wtf - I missed something??
                            }
                            break;
                        }
                    case 2:
                        {
                            nextToken += c;
                            getNextToken = true;
                            state_ = 1;
                            ++at_;
                            break;
                        }
                }
            }

            return nextToken;
        }

        public string GetNextToken(int seek)
        {
            string nextToken = "";

            for (int idx = 1; idx <= seek; ++idx)
            {
                nextToken = GetNextToken();
            }

            return nextToken;
        }

        private bool IsChar(char c)
        {
            return (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')));
        }

        private bool IsNumber(char c)
        {
            return ((c >= '0') && (c <= '9'));
        }

        private bool IsSeparatorCharacter(char c)
        {
            // here is all the separator character
            return ((c == '$') || (c == '(') || (c == ')') || (c == ':') || (c == '|') ||
                     (c == '[') || (c == ']') || (c == '#') || (c == '-') || (c == ',') || (c == '&'));
        }
    }
}
